package cn.lg.soar.database.translate;

import cn.lg.soar.common.data.dict.DictItem;
import cn.lg.soar.database.translate.mapper.TranslateMapper;
import cn.lg.soar.database.util.TableInfoPlusHelper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.Getter;
import lombok.Setter;

import java.time.Duration;
import java.util.Map;
import java.util.Objects;

/**
 * @author luguoxiang
 * @date 2022/8/12
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
class TextConfig implements IConfig{

    private String tableName;

    private String keyColumn;

    private String labelColumn;

    private TranslateMapper translateMapper;

    private Cache<Object, String> cache;

    private String defaultText;

    public TextConfig(TranslateByEntity translate, TranslateMapper translateMapper, boolean toJava) {
        this.translateMapper = translateMapper;
        TableInfo tableInfo = TableInfoHelper.getTableInfo(translate.value());
        Map<String, String> map = TableInfoPlusHelper.getMapPropertyColumn(translate.value());
        cache = Caffeine.newBuilder()
                .initialCapacity(16)
                .maximumSize(translate.cacheSize())
                .expireAfterWrite(Duration.ofSeconds(translate.cacheExpire()))
                .build();
        if (toJava) {
            keyColumn = map.get(translate.label());
            labelColumn = map.get(translate.key());
        } else {
            keyColumn = map.get(translate.key());
            labelColumn = map.get(translate.label());
        }
        tableName = tableInfo.getTableName();
        IPage<DictItem<String>> page = translateMapper.page(
                new Page<>(1, translate.cacheSize(), false),
                tableName,
                keyColumn,
                labelColumn
        );
        page.getRecords().forEach(x -> cache.put(x.getValue(), x.getLabel()));
    }

    @Override
    public String get(Object value) {
        return cache.get(value, key -> {
            String text = translateMapper.text(
                    this.tableName,
                    this.keyColumn,
                    this.labelColumn,
                    value
            );
            if (text != null) {
                return text;
            }
            return "";
        });
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        TextConfig config = (TextConfig) o;
        return Objects.equals(tableName, config.getTableName()) &&
                Objects.equals(keyColumn, config.getKeyColumn()) &&
                Objects.equals(labelColumn, config.getLabelColumn());
    }

    @Override
    public int hashCode() {
        return Objects.hash(tableName, keyColumn, labelColumn);
    }

}
